package cn.iocoder.yudao.module.common.service.likes;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.common.controller.admin.likes.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.likes.LikesDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.common.dal.mysql.likes.LikesMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.common.enums.ErrorCodeConstants.*;

/**
 * 点赞 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class LikesServiceImpl implements LikesService {

    @Resource
    private LikesMapper likesMapper;

    @Override
    public Long createLikes(LikesSaveReqVO createReqVO) {
        // 插入
        LikesDO likes = BeanUtils.toBean(createReqVO, LikesDO.class);
        likesMapper.insert(likes);
        // 返回
        return likes.getId();
    }

    @Override
    public void updateLikes(LikesSaveReqVO updateReqVO) {
        // 校验存在
        validateLikesExists(updateReqVO.getId());
        // 更新
        LikesDO updateObj = BeanUtils.toBean(updateReqVO, LikesDO.class);
        likesMapper.updateById(updateObj);
    }

    @Override
    public void deleteLikes(Long id) {
        // 校验存在
        validateLikesExists(id);
        // 删除
        likesMapper.deleteById(id);
    }

    private void validateLikesExists(Long id) {
        if (likesMapper.selectById(id) == null) {
            throw exception(LIKES_NOT_EXISTS);
        }
    }

    @Override
    public LikesDO getLikes(Long id) {
        return likesMapper.selectById(id);
    }

    @Override
    public PageResult<LikesDO> getLikesPage(LikesPageReqVO pageReqVO) {
        return likesMapper.selectPage(pageReqVO);
    }

}